﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public partial class document
    {
        public static CommonClasses.Documento[] DevolveDocumentosUser(int UserID, string Query)
        {
            try
            {
                using (DBEntity entidade = new DBEntity())
                {
                    var sel = from ent in entidade.document
                              where ent.user.id == UserID
                              select ent;
                    if (!string.IsNullOrEmpty(Query))
                    {
                        sel = from ent in sel
                              from tg in ent.tag
                              where
                              ent.name == Query ||
                              tg.name == Query
                              select ent;
                    }

                    return (from ent in sel
                            select new CommonClasses.Documento()
                            {
                                DataCriacao = ent.created_at.Value,
                                DocumentID = ent.id,
                                Favorited = ent.favorited,
                                FileName = ent.filename,
                                OriginalFileName = ent.originalFilename,
                                UserID = ent.user.id,
                                Name = ent.name,
                            }).ToArray();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }


    }
}
